package com.kum.service;

import com.alibaba.fastjson.JSON;
import com.baidu.aip.face.MatchRequest;
import com.kum.utils.BaiduFaceUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @version V1.0
 * @Package com.kum.service
 * @auhter 枯木Kum
 * @date 2021/3/9-5:42 PM
 */

@Service
public class FaceService {

    public boolean contrast(String userFaceData, String currentFaceDate) {
        // image1/image2也可以为url或facetoken, 相应的imageType参数需要与之对应。
        MatchRequest req1 = new MatchRequest(userFaceData, "BASE64");
        MatchRequest req2 = new MatchRequest(currentFaceDate, "BASE64");
        ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
        requests.add(req1);
        requests.add(req2);

        JSONObject res = BaiduFaceUtils.getInstance().match(requests);
        try {
            //todo 返回详细信息
            return JSON.parseObject(res.toString(2)).getJSONObject("result").getInteger("score") > 60;

        }catch (NullPointerException e){
            return false;
        }

    }

    public static void main(String[] args) {
        FaceService faceService = new FaceService();
//        faceService.contrast();
    }
}
